/* * =====================================================================================
 *
 *       Filename:  rule.cpp
 *
 *    Description:  Class implementation of shape rule
 *
 *        Version:  1.0
 *        Created:  30/06/12 14:00:33
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Moos Hueting (mh), mooshueting@gmail.com
 *   Organization:  UCL
 *
 * =====================================================================================
 */
#include "rule.h"
#include <time.h>
#include <iostream>

Rule::Rule(SegmentLibrary lib) {

    // select two segments at random
    int a = (int)((lib.getSize() - 1)*((float)rand()/RAND_MAX));
    int b = (int)((lib.getSize() - 1)*((float)rand()/RAND_MAX));

    // decide if generating initial or final rule
    int prob = (int)((lib.getSize() + 2)*((float)rand()/RAND_MAX));

    input = Segment(lib.getSegment(a));
    output = Segment(lib.getSegment(b));

    // decide on rotation and flip
    if (rand() % 2)
        output.setRotation(true);
    if (rand() % 2)
        output.setFlip(1);

//    output.setX((int)3*((float)rand()/RAND_MAX));
//    output.setY((int)3*((float)rand()/RAND_MAX));

    if (prob > lib.getSize()) {
        initial = true;
        final = false;
    } else if (prob == lib.getSize()) {
        initial = false;
        final = true;
    } else {
        initial = false;
        final = false;
    }
}

void Rule::printRule() {
    if (isInitial()) {
        std::cout << "INITIAL -> " << output.getName() << std::endl;
    } else if (isFinal()) {
        std::cout << input.getName() << " -> FINAL" << std::endl;
    } else {
        std::cout << input.getName() << " -> " << output.getName() << std::endl;
    }

}
